package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dao.UsersDao;
import com.zretc.emptraining.daoimpl.UsersDaoImpl;
import com.zretc.emptraining.dbtools.MD5Util;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.Users;
import com.zretc.emptraining.service.UsersService;
import com.zretc.emptraining.serviceimpl.UsersServiceImp;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 实例化接口
	UsersService usersService = new UsersServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UsersServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if ("".equals(op)) {
			// 显示列表
			doQuery(request, response);
		} else if ("addUsers".equals(op)) {
			// 添加用户
			addUsers(request, response);
		} else if ("upd".equals(op)) {
			// 修改用户状态
			updState(request,response);
		} else if ("queryByPage".equals(op)) {
			// 分页
			doQueryByPage(request, response);
		} else if ("updUser".equals(op)) {
			// 修改
			doUpdUserByName(request, response);
		} else if("initpwd".equals(op)) {
			// 初始化密码
			initPwd(request, response);
		} else if("updpwd".equals(op)) {
			// 修改密码
			updPwd(request, response);
		} else if ("getUsers".equals(op)) {
			//获得用户信息
			doGetUsers(request, response);
		} else if ("doSeekPwd".equals(op)) {
			//检查密码是否正确
			doSeelPwd(request, response);
		} else if ("upd1".equals(op)) {
			// 前台修改员工信息
			doUpd(request, response);
		}
	}
	public void doUpd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userName=request.getParameter("userName");
		String userPwds = request.getParameter("userPwds");
		System.out.println("empName:"+userName+"empAddress:"+userPwds);
		boolean flag = usersService.updPwd(userName, userPwds);
		System.out.println(flag);
		String msg = "修改成功";
		if (!flag) {
			msg = "修改失败";
		}
		Gson(msg, response);
	}
	
	public void doSeelPwd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String pwd = usersService.getUsersEmpNo(userName).getUserPwd();
		String pwds = MD5Util.getEncodeByMd5(userPwd);
		System.out.println("userPwd:"+pwds);
		System.out.println("pwd:"+pwd);
		String msg = "密码错误";
		if (pwd.equals(pwds)) {
			msg = "密码正确";
		}
		Gson(msg, response);
		System.out.println(msg);
	}
	
	public void doGetUsers(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = "";
		String empTel = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if (request.getParameter("empTel") != null) {
			empTel = request.getParameter("empTel");
		}
		List<Users> pd = usersService.getUsersByName(empTel);
		// 4传递list给jsp
		request.setAttribute("pd", pd);
		System.out.println(pd);
		// 5 转发
		request.getRequestDispatcher("pwdUpd.jsp").forward(request, response);
	}

	private void updPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userNo = request.getParameter("username");
		String userPwd = request.getParameter("pwd");
		System.out.println(userNo);
		System.out.println(userPwd);
		boolean flag = usersService.updUserPwd(userNo, userPwd);
		System.out.println(flag);
		int msg = 0;
		if(flag) {
			msg = 200;
		}
		Gson gson = new Gson();
		String toJson = gson.toJson(msg);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(toJson);
		printWriter.close();
	}

	private void initPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userNo = request.getParameter("userNo");
		boolean flag = usersService.initUserPwd(userNo);
		String msg = "初始化不成功";
		if(flag) {
			msg = "初始化成功";
		}
		Gson(msg, response);
	}

	public void doUpdUserByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userNo = request.getParameter("userNo");
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		boolean flag = usersService.updUsersByName(empNo, userName, userPwd, userNo);
		String msg = "修改成功";
		if (!flag) {
			msg = "修改失败";
		}
		Gson(msg, response);
	}

	public void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认为1，记录数为10
		int page = 1;
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用service方法
		PageData<Users> pd1 = usersService.getUsersByPage(page, pageSize, keywords);
		System.out.println("pd1:" + pd1);
		// 传递
		request.setAttribute("pd1", pd1);
		// 转发回来的时候，将查询关键字存储在request范围内
		request.setAttribute("keywords", keywords);
		// 转发
		request.getRequestDispatcher("UserList.jsp").forward(request, response);
	}

	public void updState(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String userNo = request.getParameter("userNo");
		String userState = request.getParameter("userState");
		boolean flag = usersService.stateUpd(userState, userNo);
		String msg = "修改成功";
		if (!flag) {
			msg = "修改失败";
		}
		Gson(msg, response);
	}

	public void addUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		System.out.println(empNo);
		System.out.println(userName);
		System.out.println(userPwd);
		Users users = new Users(userName, empNo, userName, userPwd, "1");
		boolean flag = usersService.addUsers(users);
		String msg = "添加成功";
		if (!flag) {
			msg = "添加失败";
		}
		Gson(msg, response);
	}

	public void doQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String keywords = "";
		// 2获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		 //3 调用 dao的方法
		List<Users> list = usersService.getUsers(keywords);
		 //4传递list给jsp
		request.setAttribute("list", list);
		// 5 转发
		request.getRequestDispatcher("UserList.jsp").forward(request, response);
	}
	public void Gson(String keyword, HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		String toJson = gson.toJson(keyword);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(toJson);
		printWriter.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
